/**
 * 
 */
package engine.expression;

import engine.expression.exceptions.IllegalActionForLeaf;
import utilities.Trio;

/**
 * @author valentin
 *
 */
public class Constante implements Expression
{
	private double value;
	private String name = null;
	
	/**
	 * 
	 */
	public Constante(double value)
	{
		this.value = value;
	}
	
	public Constante(String name)
	{
		this.name = name;
	}

	/* (non-Javadoc)
	 * @see engine.Expression#value()
	 */
	@Override
	public double value()
	{
		if(name != null) return ConstantValuation.getValue(name);
		else return value;
	}
	
	@Override
	public Operators operator()
	{
		throw new IllegalActionForLeaf();
	}
	
	public String name()
	{
		return name;
	}
	
	public String toString()
	{
		if(name == null)
			return Double.toString(value());
		else
			return name;
	}
	
	@Override
	public boolean isCalculable()
	{
		if(name != null)
			return ConstantValuation.hasValue(name);
		else
			return true;
	}

	@Override
	public Trio<Operators, Double, Expression> reduceNumber()
	{
		Expression exp = null;
		double number = 0;
		
		if(name != null)
		{
			if(ConstantValuation.hasValue(name))
				number = ConstantValuation.getValue(name);
			else
				exp = this;
		}
		else
			number = value;
		
		return new Trio<Operators, Double, Expression>(null, number, exp);
	}

	@Override
	public boolean isConstante()
	{
		return true;
	}
}
